import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

// import login from "@/views/Login";
// import Home from "@/views/Layout/Home";
// import User from "@/views/Layout/User";
// import Layout from "@/views/Layout";
// import SearchBar from "@/views/Layout/Search";
// import SearchResult from "@/views/Layout/Search/SearchResult.vue";
// import Article from "@/views/ArticleDetail";
// import UserEdit from "@/views/Layout/User/userEdit.vue";
// import ChatRobot from "@/views/chat";
//关联 把路由和页面组件关联在一起
const routes = [
  {
    path: "/",
    redirect: "/Layout",
  },
  {
    path: "/chat",
    // component: ChatRobot,
    component: () => import(/* webpackChunkName: "chat" */ "@/views/chat"),
  },
  // 文章詳情
  {
    path: "/article",
    // component: Article,
    component: () =>
      import(/* webpackChunkName: "article" */ "@/views/ArticleDetail"),
  },
  // 用户编辑
  {
    path: "/useredit",
    // component: UserEdit,
    component: () =>
      import(/* webpackChunkName: "useredit" */ "@/views/User/userEdit.vue"),
  },
  {
    path: "/searchresult/:value",
    // component: SearchResult,
    component: () =>
      import(
        /* webpackChunkName: "searchresult" */ "@/views/Search/SearchResult.vue"
      ),
  },
  {
    path: "/login",
    // component: login,
    component: () => import(/* webpackChunkName: "login" */ "@/views/Login"),
    beforeEnter: (from, to, next) => {
      // 在有token时防止跳转到登录页面;
      if (localStorage.getItem("token")?.length > 0) {
        console.log("有token不放行");
        // return false;
        // next(fasle)
        next("/layout/home");
      } else {
        next();
      }
    },
  },
  {
    path: "/search",
    // component: SearchBar,
    component: () => import(/* webpackChunkName: "search" */ "@/views/Search"),
  },
  {
    path: "/layout",
    // component: Layout,
    component: () => import(/* webpackChunkName: "layout" */ "@/views/Layout"),
    redirect: "/layout/home",
    children: [
      {
        path: "home",
        // component: Home,
        component: () => import(/* webpackChunkName: "home" */ "@/views/Home"),
        meta: {
          scrollTop: 0, //保存首页滚动条的位置
        },
      },
      {
        path: "user",
        // component: User,
        component: () => import(/* webpackChunkName: "user" */ "@/views/User"),
      },
    ],
  },
];
// 创建路由对象, 传入规则
const router = new VueRouter({ routes });

// // 全局路由守卫
// router.beforeEach((to, from, next) => {
//   // 在有token时防止跳转到登录页面;
//   if (localStorage.getItem("token")?.length > 0 && to.path == "/login") {
//     next(false);
//     console.log("在有token时防止跳转到登录页面;");
//   } else {
//     console.log("放行next()");
//     next();
//   }
// });
export default router;
